package com.zlzlib.libscanqr;


import android.app.Activity;
import android.widget.Toast;


import com.zlzlib.libscan.BaseScanView;
import com.zlzlib.libscan.Utils;


/**
 * Created by zlz on 2017/10/24.
 * 二维码扫描页面  使用实列
 * <p>
 * < QrScanView
 * android:id="@+id/scan_view"
 * android:layout_width="match_parent"
 * android:layout_height="match_parent"
 * app:anim_time="1200"
 * app:box_h="300dp"
 * app:box_stroke="1dp"
 * app:box_top_offset="-10dp"
 * app:box_w="300dp"
 * app:con_length="12dp"
 * app:con_stroke="3dp"
 * app:is_box_center="true"
 * app:is_only_scan_area="true"
 * app:is_scan_move_back="true"
 * app:is_tip_bg_fixed_w="true"
 * app:is_tip_top="true"
 * app:tip_text_margin="10dp"
 * app:tip_text_size="13sp" />
 */

public abstract class DemoQrScanActivity extends Activity {

    public static final String IS_BAR_SCAN = "is_bar_scan";
    public static final String IS_ONLY_QR_CODE = "is_only_qr_code";
    //是否只扫条码
    private boolean isBar = false;
    //是否只扫二维码
    private boolean isOnlyQrCode = true;

    protected abstract void closeSetResult(String result);

    private QrScanView scanView;

    /**
     * 初始化扫码控件
     *
     * @param qrScanView 扫码控件
     */
    protected void initScanView(QrScanView qrScanView) {
        scanView = qrScanView;
        scanView.setResultListener(new BaseScanView.ResultListener() {
            @Override
            public void onScanSuccess(String result) {
                if (result == null) {
                    Toast.makeText(getBaseContext(), getString(R.string.scan_fail), Toast.LENGTH_SHORT).show();
                    return;
                }
                closeSetResult(result);
            }

            @Override
            public void onScanOpenCameraError() {
                Toast.makeText(getBaseContext(), getString(R.string.open_scan_fail), Toast.LENGTH_SHORT).show();
            }
        });
        isBar = getIntent().getBooleanExtra(IS_BAR_SCAN, isBar);
        isOnlyQrCode = getIntent().getBooleanExtra(IS_ONLY_QR_CODE, isOnlyQrCode);
        if (isBar) {
            //设置
            scanView.getScanBox().setBoxWH(Utils.dip2px(getBaseContext(), 300), Utils.dip2px(getBaseContext(), 150)); // 切换成扫描条码样式
            scanView.setType(QrCodeType.ONE_DIMENSION, null); // 只识别一维条码
            scanView.getScanBox().setTipText(getString(R.string.put_bar_scan));
        } else {
            scanView.getScanBox().setBoxWH(Utils.dip2px(getBaseContext(), 300), Utils.dip2px(getBaseContext(), 300));// 切换成扫描二维码样式
            if (isOnlyQrCode) {
                scanView.setType(QrCodeType.TWO_DIMENSION, null); // 只识别二维条码
                scanView.getScanBox().setTipText(getString(R.string.put_z_bar_scan));
            } else {
                scanView.setType(QrCodeType.ALL, null); //识别条码和二维码
                scanView.getScanBox().setTipText(getString(R.string.put_zx_bar_scan));
            }
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        scanView.onStartCamera();
    }

    @Override
    protected void onStop() {
        super.onStop();
        scanView.onStopCamera();
    }

    @Override
    protected void onDestroy() {
        scanView.onDestroyCamera();
        super.onDestroy();
    }

}
